package com.ctbri.manage.quality.config;
import java.io.*;
import java.util.*;

//import com.ctc.wstx.api.ReaderConfig;
import lombok.Setter;
import org.jcodings.util.Hash;
import org.yaml.snakeyaml.Yaml;
import lombok.Getter;

public class YamlReader{
    @Getter @Setter private Map<String, Object> conf;
    public Map<String, Object> getConfForScala(){
        return this.conf;
    }
    public void setConfForScala(final Map<String, Object> conf){
        this.conf = conf;
    }
    private static YamlReader instance;

    private YamlReader(final String filePath){
      /*  try{
            this.conf = (Map<String, Object>) new Yaml().load(new FileInputStream(new File(filePath)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
    }

    public static YamlReader getInstance(final String filePath){
       /* if (YamlReader.instance == null){
            synchronized (ReaderConfig.class){
                if (YamlReader.instance == null){
                    YamlReader.instance = new YamlReader(filePath);
                }
            }
        }*/
        return YamlReader.instance;
    }

    //读取String类型
    public String getString(String param){
        return String.valueOf(this.conf.get(param));
    }

    public String getString(String param, String defaultValue){
        if (null == this.conf.get(param)) {
            return defaultValue;
        }
        return String.valueOf(this.conf.get(param));
    }

    //读取Integer类型
    public Integer getInteger(String param, Boolean isString){
        /*
        @isString: yaml文件中Integer是否以String的形式写入
         */
        if (isString) return Integer.valueOf((String) this.conf.get(param));
        else return (Integer) this.conf.get(param);
    }
    public Integer getInteger(String param, Boolean isString, Integer defaultValue){
        if (null == this.conf.get(param)) {
            return defaultValue;
        }
        if (isString) return Integer.valueOf((String) this.conf.get(param));
        else return (Integer) this.conf.get(param);
    }

    //读取Double类型
    public Double getDouble(String param, Boolean isString){
        if (isString) return Double.valueOf((String) this.conf.get(param));
        else return (Double) this.conf.get(param);
    }
    public Double getDouble(String param, Boolean isString, Double defaultValue){
        if (null == this.conf.get(param)) {
            return defaultValue;
        }
        if (isString) return Double.valueOf((String) this.conf.get(param));
        else return (Double) this.conf.get(param);
    }

    //读取Boolean类型
    public Boolean getBoolean(String param, Boolean isString){
        if (isString) return Boolean.valueOf((String) this.conf.get(param));
        else return (Boolean) this.conf.get(param);
    }
    public Boolean getBoolean(String param, Boolean isString, Boolean defaultValue){
        if (null == this.conf.get(param)) {
            return defaultValue;
        }
        if (isString) return Boolean.valueOf((String) this.conf.get(param));
        else return (Boolean) this.conf.get(param);
    }

    //读取ArrayList<E>类型，E为基础类型Integer，String，Double，Boolean
    public <E> ArrayList<E> getList(String param){
        return (ArrayList<E>) this.conf.get(param);
    }
    public <E> ArrayList<E> getList(String param, ArrayList<E> defaultValue){
        if (null == this.conf.get(param)){
            return defaultValue;
        }
        return (ArrayList<E>) this.conf.get(param);
    }

    //读取ArrayList<ArrayList<E>>类型，E为基础类型Integer，String，Double，Boolean
    public <E> ArrayList<ArrayList<E>> get2dimList(String param){
        ArrayList<ArrayList<E>> result = new ArrayList<>();
        for (ArrayList<E> l: (ArrayList<ArrayList<E>>) this.conf.get(param)){
            result.add(l);
        }
        return result;
    }
    public <E> ArrayList<ArrayList<E>> get2dimList(String param, ArrayList<ArrayList<E>> defaultValue){
        if (this.conf.get(param) == null) return defaultValue;
        ArrayList<ArrayList<E>> result = new ArrayList<>();
        for (ArrayList<E> l: (ArrayList<ArrayList<E>>) this.conf.get(param)){
            result.add(l);
        }
        return result;
    }

    /**
     * 读取Map<String, E>类型，E为基础类型Integer，String，Double，Boolean
     * @param param
     * @return
     * @param <E>
     */
    public <E> HashMap<String, E> getMap(String param){
        HashMap<String, E> result = new HashMap<>();
        for (String key: ((HashMap<String, E>)this.conf.get(param)).keySet()){
            result.put(key, ((HashMap<String, E>)this.conf.get(param)).get(key));
        }
        return result;
    }
    /**
     * 读取HashMap<String, HashMap<String, E>>类型，E为基础类型
     * @param param
     * @return
     * @param <E>
     */
    public <E> HashMap<String, HashMap<String, E>> get2dMap(String param){
        HashMap<String, HashMap<String, E>> result = new HashMap<>();
        for (String key: ((HashMap<String, E>)this.conf.get(param)).keySet()){
            result.put(key, (HashMap<String, E>) ((HashMap<String, E>)this.conf.get(param)).get(key));
        }
        return result;
    }
    //todo: 碰到更复杂的类型需要补充
}
